package com.icepeach.icepeachhomeback.controller;

import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.SecureUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.icepeach.icepeachhomeback.entity.Article;
import com.icepeach.icepeachhomeback.req.ArticleListQueryReq;
import com.icepeach.icepeachhomeback.service.ArticleService;
import com.icepeach.icepeachhomeback.util.ApiResult;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@Controller
@RequestMapping("/article")
public class ArticleController {
    @Resource
    ArticleService articleService;

    /**
     * 获取所有用户
     *
     * @return
     */
    @PostMapping("/getAllArticle")
    @ResponseBody
    public ApiResult getAllArticle() {
        List<Article> list = articleService.list(null);
        return ApiResult.success("查询成功", list);
    }

    /**
     * 根据id获取单个文章
     *
     * @return
     */
    @PostMapping("/getArticleById")
    @ResponseBody
    public ApiResult getArticleById(String id) {
        if (StrUtil.isNotBlank(id)) {
            return ApiResult.success("查询成功", articleService.getById(id));
        } else {
            return ApiResult.failure("请求异常");
        }
    }
    /**
     * 根据文章名称获取单个文章
     *
     * @return
     */
    @PostMapping("/getArticleByTitle")
    @ResponseBody
    public ApiResult getArticleByName(@RequestBody Article article) {
        if (StrUtil.isNotBlank(article.getTitle())) {
            return ApiResult.success("查询成功", articleService.getOne(new QueryWrapper<Article>().eq("title",article.getTitle())));
        } else {
            return ApiResult.failure("请求异常");
        }
    }


    /**
     * 分页查询 获取用户
     *
     * @param articleListQueryReq
     * @return
     */
    @PostMapping("/getArticlesByPage")
    @ResponseBody
    public ApiResult getArticlesByPage(@RequestBody ArticleListQueryReq articleListQueryReq) {

        return ApiResult.success("查询成功", articleService.getArticlesByPage(articleListQueryReq));
    }

    /**
     * 增加或修改用户
     *
     * @param article
     * @return
     */

    @PostMapping("/saveArticle")
    @ResponseBody
    public ApiResult saveArticle(@RequestBody Article article) {
        boolean result = articleService.saveOrUpdate(article);
        if (result) {
            return ApiResult.success("成功");
        } else {
            return ApiResult.failure("失败");
        }
    }

    @PostMapping("/deleteById")
    @ResponseBody
    public ApiResult deleteById(@RequestBody Article article) {
        boolean result = articleService.removeById(article.getId());

        if (result) {
            return ApiResult.success("成功");
        } else {
            return ApiResult.failure("失败");
        }
    }

    @PostMapping("/del/batch")
    @ResponseBody
    public ApiResult deleteBatch(@RequestBody List<String> ids) {
        articleService.removeByIds(ids);
        return ApiResult.success("成功");
    }
}
